package com.kzd.sdk.ui.fragment;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnAccessListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.utils.AppUtils;

public class KZD_AccessEnrollFragment extends OwnerFragment {

    private LinearLayout llAccessEnrolCode;
    private TextView tvAccountEnrolSubmit, tvAccessEnrolCodeGet;
    private EditText etAccessEnrolAccount, etAccessEnrolPass, etAccessEnrolCardName, etAccessEnrolCardCode, etAccessEnrolCode;

    private String mSessionId = "";

    private final CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {
        public void onTick(long millisUntilFinished) {
            String text = "剩余" + millisUntilFinished / 1000 + "秒";
            tvAccessEnrolCodeGet.setText(text);
        }

        public void onFinish() {
            tvAccessEnrolCodeGet.setTextColor(Color.parseColor("#2983FF"));
            tvAccessEnrolCodeGet.setEnabled(true);
            tvAccessEnrolCodeGet.setText("获取验证码");
        }
    };

    private final KZD_HttpCallback<String> callbackPhoneCode = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSessionId = data;
            mCountDownTimer.start();
            tvAccessEnrolCodeGet.setTextColor(Color.parseColor("#999999"));
            tvAccessEnrolCodeGet.setEnabled(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    public KZD_OnAccessListener mOnAccessListener;

    public void setOnAccessListener(KZD_OnAccessListener onAccessListener) {
        this.mOnAccessListener = onAccessListener;
    }

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_access_enrol";
    }

    @Override
    public void initView(View view) {
        llAccessEnrolCode = view.findViewById(AppUtils.getId(mContext, "llAccessEnrolCode"));
        tvAccessEnrolCodeGet = view.findViewById(AppUtils.getId(mContext, "tvAccessEnrolCodeGet"));
        etAccessEnrolCode = view.findViewById(AppUtils.getId(mContext, "etAccessEnrolCode"));
        etAccessEnrolAccount = view.findViewById(AppUtils.getId(mContext, "etAccessEnrolAccount"));
        etAccessEnrolPass = view.findViewById(AppUtils.getId(mContext, "etAccessEnrolPass"));
        etAccessEnrolCardName = view.findViewById(AppUtils.getId(mContext, "etAccessEnrolCardName"));
        etAccessEnrolCardCode = view.findViewById(AppUtils.getId(mContext, "etAccessEnrolCardCode"));

        tvAccountEnrolSubmit = view.findViewById(AppUtils.getId(mContext, "tvAccountEnrolSubmit"));

        etAccessEnrolAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (AppUtils.isMobileExact(s)) {
                    llAccessEnrolCode.setVisibility(View.VISIBLE);
                } else {
                    mCountDownTimer.cancel();
                    mSessionId = "";
                    etAccessEnrolCode.setText(null);
                    tvAccessEnrolCodeGet.setTextColor(Color.parseColor("#2983FF"));
                    tvAccessEnrolCodeGet.setEnabled(true);
                    tvAccessEnrolCodeGet.setText("获取验证码");
                    llAccessEnrolCode.setVisibility(View.GONE);
                }
            }
        });

        tvAccessEnrolCodeGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etAccessEnrolAccount.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else {
                    KZD_ApiManager.httpPhoneCode(phone, 0, callbackPhoneCode);
                }
            }
        });

        //注册进入游戏点击事件
        tvAccountEnrolSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitRegisterAndGame();
            }
        });

        ImageView ivAccessEnrolBack = view.findViewById(AppUtils.getId(mContext, "ivAccessEnrolBack"));
        ivAccessEnrolBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });
    }

    /**
     * 注册并进入游戏
     */
    private void submitRegisterAndGame() {
        if (TextUtils.isEmpty(etAccessEnrolAccount.getText())) {
            Toast.makeText(mContext, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (AppUtils.isMobileExact(etAccessEnrolAccount.getText()) && TextUtils.isEmpty(etAccessEnrolCode.getText())) {
            Toast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(etAccessEnrolPass.getText())) {
            Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(etAccessEnrolCardName.getText())) {
            Toast.makeText(mContext, "请输入真实姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(etAccessEnrolCardCode.getText())) {
            Toast.makeText(mContext, "请输入身份证号码", Toast.LENGTH_SHORT).show();
            return;
        }
        String type = "0";
        if (AppUtils.isMobileExact(etAccessEnrolAccount.getText())) {
            type = "1";
        } else {
            type = "0";
        }
        if (mOnAccessListener == null) {
            return;
        }
        mCountDownTimer.cancel();
        mOnAccessListener.onRegister(
                etAccessEnrolAccount.getText().toString(), etAccessEnrolPass.getText().toString(),
                etAccessEnrolCode.getText().toString(), mSessionId, type, etAccessEnrolCardName.getText().toString(),
                etAccessEnrolCardCode.getText().toString()
        );
    }

}
